package fr.upmc.i3s.fuglmeyeranalyser.view.menus;

import java.awt.GridLayout;
import java.util.LinkedList;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import fr.upmc.i3s.fuglmeyeranalyser.patients.PatientParameter;
import fr.upmc.i3s.fuglmeyeranalyser.patients.PrivatePatient;
import fr.upmc.i3s.fuglmeyeranalyser.patients.PrivatePatientList;
import fr.upmc.i3s.fuglmeyeranalyser.tools.xls.XLSKernelFileContent;

public class MenuKernelId extends JPanel  {

	private static final long serialVersionUID = -5852480518070583350L;
	
	private Object[][] datas;

	/**
	 * Create the panel.
	 */
	public MenuKernelId(XLSKernelFileContent kernelFileContent) {
		super(new GridLayout(0, 1));
		setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
		JTable table = createTable(kernelFileContent);
		JScrollPane scrollpane = new JScrollPane();
		scrollpane.setViewportView(table);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		add(scrollpane);
	}
	
	public JTable createTable(XLSKernelFileContent kernelFileContent) {
		int optionalParamsNb = kernelFileContent.getParametersNumber();
		int patientNb = kernelFileContent.getPatientNumber();
		System.out.println("patientNb="+patientNb+", optionalParamsNb="+optionalParamsNb);
		
		// build entetes
		LinkedList<String> params = kernelFileContent.getParamsList();
		String[] entetes = new String[optionalParamsNb]; 
		int cpt = 0;
		for(String param : params) {
			entetes[cpt] = param;
			System.out.println("param name = "+entetes[cpt]);
			cpt++;
		}
		
		// build data matrix
		datas = new Object[patientNb][optionalParamsNb];
		int i = 0;
		PrivatePatientList patients = kernelFileContent.getPatientList();
		for(PrivatePatient patient : patients) {
			cpt = 0;
			Object[] patientData = new Object[optionalParamsNb];
			LinkedList<PatientParameter> patientParams = patient.getParamsList();
			for(PatientParameter patientParam : patientParams) {
				patientData[cpt] = patientParam.getValue();
				cpt++;
			}
			datas[i] = patientData;
			i++;
		}
		
		// build table
		JTable table = new JTable(datas, entetes);
		table.getTableHeader().setReorderingAllowed(false);
		return table;
	}
	
	public void setData(Object[][] donnees) {
		this.datas = donnees;
	}

}
